// https://zhuanlan.zhihu.com/p/22610408

var path = require('path');
var cooking = require('cooking');
var App = require('./app.json');

// 入口文件
var entries = function() {
  var result = {}
  App.pages.forEach(p => {
    result[p.entry] = path.resolve(App.basePath, p.entry)
  })
  return result
};

var merge = function(a, b) {
  return {
    css: (a.css || []).concat(b.css || []),
    js: (a.js || []).concat(b.js || [])
  }
};
var templates = function() {
  return App.pages.map(p => {
    return {
      title: p.title,
      filename: p.entry + '.html',
      template: path.resolve(__dirname, 'index.tpl'),
      cdn: merge(App.cdn, p.cdn),
      chunks: ['vendor', 'manifest', p.entry]
    }
  })
};

cooking.set({
  // entry: {
  //   app: ['babel-polyfill', './src/main.js']
  // },
  entry: entries(),
  dist: './dist',
  // template: './index.tpl',
  template: templates(),

  devServer: {
    port: 8080,
    publicPath: '/'
  },

  // production
  clean: true,
  hash: false,
  sourceMap: false,
  minimize: true,
  chunk: true, // see https://cookingjs.github.io/zh-cn/configuration.html#chunk
  postcss: [
    // require('...')
  ],
  publicPath: '/',
  assetsPath: 'static',
  urlLoaderLimit: 10000,
  // extractCSS: '[name].[contenthash:7].css',
  extractCSS: 'css/style.css',
  alias: {
    'src': path.join(__dirname, 'src')
  },
  extends: ['vue2', 'lint', 'sass', 'autoprefixer']
});

cooking.add('output.filename', 'js/[name].bundle.js');

module.exports = cooking.resolve();
